home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / virus / secdr13a.arj / LOGIN.C < prev    next >
C/C++ Source or Header  |  1994-01-29  |  12KB  |  479 lines

  1. /* Secure Drive LOGIN V1.3A */
  2. /* Logs into encrypted disks */
  3.  
  4. #include "secdrv.h"
  5.  
  6. extern char pass1[MAXPASS];
  7. extern char pass2[MAXPASS];
  8. extern char Old13Check[4];
  9.  
  10. extern int  tsr_not_installed;
  11. extern int  tsr_wrong_version;
  12. extern int  df10,ef10;
  13. extern char compat_mode;
  14.  
  15. int   setenv(char*,char *);
  16. void  clrbufs(void);
  17. char *pgpv;
  18.  
  19. int main(int argc,char *argv[])
  20. {
  21. unsigned drive,firstcyl,firsthead,maxcyl;
  22. unsigned maxhead,maxsector,secsize,i;
  23. unsigned safemode=FALSE;
  24. unsigned pgpsw=FALSE;
  25. unsigned char buf[512],key[16],check[4];
  26. word16 expkey[52];
  27. unsigned serial[2];
  28. unsigned char *p;
  29. char drvltr;
  30. int ekeyexp=0;
  31.  
  32. if(argc==1)
  33.  {
  34.     printf("\n\
  35. Secure Drive Login Version 1.3A\n\
  36. This program sets parameters and loads passphrases for encrypted\
  37.  drives.\n\n\
  38. LOGIN /F  [/PGP]                 to enter floppy disk passphrase\n\
  39.                                  [use/set PGPPASS]\n\
  40. \n\
  41. LOGIN /C  [/PGP]                 to erase keys and disable encryption\n\
  42.                                  [clear PGPPASS]\n\
  43. \n\
  44. LOGIN drive letter  [/PGP]       to activate an encrypted hard drive\n\
  45.                                  [use/set PGPPASS]\n\
  46. LOGIN D:    (example)\n\
  47. LOGIN D: /S                      to prevent accidental access to an\n\
  48.                                  encrypted drive without logging in\n\
  49. \n\
  50. LOGIN /PGP                       to set PGPPASS environment variable\n\
  51. \n\
  52. LOGIN drive cylinder head        to manually enter parameters for\n\
  53.                                  a hard drive partition\n\
  54. LOGIN drive cylinder head /S     to prevent accidental access\n\
  55. LOGIN 0 100 1 (example)\n\
  56.       drives are numbered from zero\n\n");
  57.     exit(1);
  58.   }
  59.  
  60.  
  61. for (i=1;i<argc;i++)
  62.  {
  63.    if (stricmp(argv[i],"/PGP") == 0)
  64.     {
  65.      pgpsw=TRUE;
  66.      pgpv=getenv("PGPPASS");
  67.      break;
  68.     }
  69.  }
  70.  
  71. if (argc == 2 && pgpsw)
  72.  {
  73.   printf("\nEnter PGPPASS passphrase for PGP Secret Key: ");
  74.   invgets(pass1);
  75.   if(pass1[0] != 0)
  76.    {
  77.     if (setenv("PGPPASS",pass1) == 0)
  78.      {
  79.       printf("PGPPASS set. \n");
  80.       clrbufs();
  81.       exit(0);
  82.      }
  83.     else
  84.      {
  85.       printf("PGPPASS not set. \n");
  86.       clrbufs();
  87.       exit(1);
  88.      }
  89.    }
  90.   else
  91.    {
  92.     if (pgpv != NULL)
  93.      {
  94.       for (i=0;i<strlen(pgpv);i++)
  95.        pass1[i]='x';
  96.       pass1[i]=0;
  97.       setenv("PGPPASS",pass1);
  98.       setenv("PGPPASS","");
  99.       printf("PGPPASS removed from environment\n");
  100.       clrbufs();
  101.       exit(0);
  102.      }
  103.     else
  104.      {
  105.       printf("PGPPASS not removed from environment because not present\n");
  106.       clrbufs();
  107.       exit(1);
  108.      }
  109.    }
  110.  }
  111.  
  112. if(!(cryptdata=gettsradr()))
  113.  {
  114.   printf("\nError: Secure Drive TSR %s.\n",
  115.          tsr_wrong_version ? "wrong Version" : "not loaded");
  116.   exit(1);
  117.  }
  118.  
  119.  
  120.  
  121. if((*argv[1]=='/')&&(toupper(*(argv[1]+1))=='C'))
  122. {
  123.   bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  124.   for (i=0;i<MAXDRV;i++)
  125.    cryptdata->hd[i].active=0;
  126.  
  127.   memset(cryptdata->fkey,0xaa,104);
  128.   memset(cryptdata->hkey,0xbb,104);
  129.   memset(cryptdata->fkeychk,0x0ff,4);
  130.   memset(cryptdata->hkeychk,0x0ff,4);
  131.   printf("\nClearing free memory...");
  132.   while ((p=malloc(1024)) != NULL)
  133.    {
  134.     memset(p,0,1024);
  135.    }
  136.   while ((p=malloc(8)) != NULL)
  137.    {
  138.     memset(p,0,8);
  139.    }
  140.  
  141.    printf("\nAll keys erased. System secured.\n");
  142.    if (pgpsw && pgpv != NULL)
  143.     {
  144.      for (i=0;i<strlen(pgpv);i++)
  145.       pass1[i]='x';
  146.      pass1[i]=0;
  147.      setenv("PGPPASS",pass1);
  148.      setenv("PGPPASS","");
  149.      printf("PGPPASS removed from environment\n");
  150.     }
  151.   clrbufs();
  152.   exit(0);
  153.  }
  154.  
  155. set_compat_mode();
  156.  
  157. if((*argv[1]=='/')&&(toupper(*(argv[1]+1))=='F'))
  158.  {
  159.   char c;
  160.   int drive;
  161.   int fdpr=1;
  162.   while (1)
  163.    {
  164.     printf("\nInsert encrypted diskette in drive A or B\n\
  165. then press A or B to check passphrase.\n\
  166. Or Press Esc to bypass passphrase check.: ");
  167.     while ((c=toupper(getch())) != 'A' && c != 'B' && c != ESC) {}
  168.     if (c == ESC) break;
  169.     printf("%c\n\n",c);
  170.     drvltr=c;
  171.     drive=(unsigned) c-'A';
  172.     readsec(drive,0,0,1,1,buf);
  173.     if((buf[510]!=0x55)||(buf[511]!=0xaa))
  174.      {
  175.       printf("This diskette is not formatted.\n\n");
  176.       continue;
  177.      }
  178.     if(memcmp(buf+3,"CRYP",4) != 0)
  179.      {
  180.       printf ("This diskette is not encrypted.\n\n");
  181.       continue;
  182.      }
  183.     break;
  184.    } /* while (1)*/
  185.  
  186.   {
  187.    unsigned char temp[16];
  188.    unsigned char nokey [104];
  189.    MD5_CTX md5buf;
  190.  
  191.    memset(nokey,0xbb,104);
  192.    if (memcmp(cryptdata->hkey,nokey,104) != 0)
  193.     {
  194.      if (c != ESC)
  195.       {
  196.        if(memcmp(cryptdata->hkeychk,buf+7,4) == 0)
  197.         {
  198.          memcpy(check,cryptdata->hkeychk,4);
  199.          memcpy(expkey,cryptdata->hkey,104);
  200.          ef10 = df10 = cryptdata->fkeyv10 = cryptdata->hkeyv10;
  201.          ekeyexp=1;
  202.          fdpr=0;
  203.          printf("Floppy disk encryption enabled with\n\
  204. verified active hard disk partition %s passphrase.\n",
  205.           df10 ? "(V 1.0)" : "(V 1.1)");
  206.  
  207.         }
  208.       }
  209.      else /*verify bypassed*/
  210.       {
  211.        printf("Use active Hard Disk passphrase as unverified floppy passphrase? :");
  212.        if (getyn())
  213.         {
  214.          memcpy(check,cryptdata->hkeychk,4);
  215.          memcpy(expkey,cryptdata->hkey,104);
  216.          ef10 = cryptdata->hkeyv10;
  217.          ekeyexp=1;
  218.          fdpr=0;
  219.          printf("Floppy disk encryption enabled with\n\
  220. unverified active hard disk partition %s passphrase.\n",
  221.           ef10 ? "(V 1.0)" : "(V 1.1)");
  222.         }
  223.       }
  224.     }
  225.   }
  226.  
  227.    if (fdpr && pgpsw && pgpv != NULL)
  228.     {
  229.      strcpy(pass1,pgpv);
  230.      if (c != ESC)
  231.       {
  232.        setkeydf(key,check,buf+7);
  233.        if(!df10 && !memcmp(Old13Check,buf+7,4))
  234.         {
  235.          printf("Check bytes in Disk %c: Boot Sector need updating from 1.3 to 1.1/1.3A. Proceed? ",
  236.                 drvltr);
  237.          if(getyn())
  238.           {
  239.            memcpy(buf+7,check,4);
  240.            writesec(drive,0,0,1,1,buf);
  241.           }
  242.         }
  243.  
  244.        ef10=df10;
  245.        if(memcmp(check,buf+7,4) == 0)
  246.         {
  247.          printf("\nPGPPASS entered as %s floppy disk passphrase\n",
  248.                df10 ? "(V 1.0)" : "(V 1.1)");
  249.          fdpr=0;
  250.         }
  251.        else
  252.         {
  253.          printf("\nPGPPASS is wrong passphrase.\n");
  254.          fdpr=1;
  255.         }
  256.  
  257.       }
  258.      else
  259.       {
  260.        setkeye (key,check);
  261.        printf("\nPGPPASS entered as %s unverified floppy passphrase\n",
  262.                ef10 ? "(V 1.0)":"(V 1.1)");
  263.        fdpr=0;
  264.       }
  265.     }
  266.    if (fdpr)
  267.     {
  268.      printf("\nEnter floppy disk passphrase: ");
  269.      if (c != ESC)
  270.       {
  271.        getkeydf(key,check,buf+7);
  272.        if(!df10 && !memcmp(Old13Check,buf+7,4))
  273.         {
  274.          printf("Check bytes in Disk %c: Boot Sector need updating from 1.3 to 1.1/1.3A. Proceed? ",
  275.                 drvltr);
  276.          if(getyn())
  277.           {
  278.            memcpy(buf+7,check,4);
  279.            writesec(drive,0,0,1,1,buf);
  280.           }
  281.         }
  282.        ef10=df10;
  283.        if(memcmp(check,buf+7,4) == 0)
  284.         {
  285.          printf("Floppy disk encryption enabled with\n");
  286.          printf("verified %s passphrase.\n",
  287.                  df10 ? "(V 1.0)" : "(V 1.1)");
  288.         }
  289.        else
  290.         {
  291.          printf("Wrong passphrase.\n");
  292.          exit(1);
  293.         }
  294.       }
  295.      else
  296.       {
  297.        getkey(key,check,FALSE);
  298.        printf("Floppy disk encryption enabled with\n");
  299.        printf("Unverified %s passphrase.\n",
  300.                ef10 ? "(V 1.0)":"(V 1.1)");
  301.       }
  302.     }
  303.    if (!ekeyexp)
  304.     en_key_idea((word16 *) key,expkey);
  305.    memcpy(cryptdata->fkey,expkey,104);
  306.    memcpy(cryptdata->fkeychk,check,4);
  307.    cryptdata->fkeyv10=(char) ef10;
  308.    cryptdata->fda.firstcyl=0;
  309.    cryptdata->fdb.firstcyl=0;
  310.    if (pgpsw && pgpv == NULL && setenv("PGPPASS",pass1) == 0)
  311.     printf("PGPPASS set. \n");
  312.    bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  313.    clrbufs();
  314.    exit(0);
  315.  }
  316.  
  317. else if(isalpha(*argv[1]))
  318.  {
  319.   drvltr=toupper(*argv[1]);
  320.   drive=255;
  321.   readptbl(drvltr,&drive,&firsthead,&firstcyl);   /*1.1*/
  322.   if(drive==255) {
  323.       printf("\nDrive not found.\n");
  324.       exit(1); }
  325.   if((*argv[2]=='/')&&(toupper(*(argv[2]+1))=='S'))
  326.       safemode=TRUE;
  327.   printf("\nDrive %c is physical hard drive %u, head %u,\
  328.  cylinder %u\n",drvltr,drive,firsthead,firstcyl);
  329.  }
  330.  
  331. else
  332.  {
  333.   drvltr='X';
  334.   i=sscanf(argv[1],"%u",&drive);
  335.   i=i&&sscanf(argv[2],"%u",&firstcyl);
  336.   i=i&&sscanf(argv[3],"%u",&firsthead);
  337.   if((*argv[4]=='/')&&(toupper(*(argv[4]+1))=='S'))
  338.       safemode=TRUE;
  339.   if(!i)
  340.    {
  341.     printf("Incorrect drive, cylinder, or head input.\n\
  342. Run without a command line for help.\n");
  343.     exit(1);
  344.    }
  345.  }
  346.  
  347. drive+=0x80;
  348.  
  349. readsec(drive,firsthead,firstcyl,1,1,buf);
  350.  
  351. if((buf[510]!=0x55)||(buf[511]!=0xaa)) {
  352.     printf("This is not a boot sector.\n");
  353.     exit(1); }
  354.  
  355. calcdiskparams(buf,&maxcyl,&maxhead,&maxsector,
  356.                &secsize,serial);
  357.  
  358. printf("Disk %c: has: %i cylinders, %i sectors, %i heads, sector \
  359. size %i bytes\n",drvltr,maxcyl+1,maxsector,maxhead,secsize);
  360.  
  361. if(memcmp("CRYP",buf+3,4)) {
  362.     printf("This disk is not encrypted.\n\n");
  363.     exit(1); }
  364.  
  365. if(!safemode)
  366. {
  367.  int hdpr=1;
  368.  if (pgpsw && pgpv != NULL)
  369.   {
  370.    strcpy(pass1,pgpv);
  371.    setkeydf(key,check,buf+7);
  372.    if(!df10 && !memcmp(Old13Check,buf+7,4))
  373.     {
  374.      printf("Check bytes in Disk %c: Boot Sector need updating from 1.3 to 1.1/1.3A. Proceed? ",
  375.             drvltr);
  376.      if(getyn())
  377.       {
  378.        memcpy(buf+7,check,4);
  379.        writesec(drive,firsthead,firstcyl,1,1,buf);
  380.       }
  381.     }
  382.    if(memcmp(check,buf+7,4))
  383.     {
  384.      printf("\nPGPPASS is wrong passphrase.\n");
  385.      hdpr=1;
  386.     }
  387.    else
  388.     {
  389.      printf("\nPGPPASS entered as %s hard disk passphrase.\n",
  390.             df10 ? "(V 1.0)" : "(V 1.1)");
  391.      hdpr=0;
  392.     }
  393.   }
  394.  if (hdpr)
  395.   {
  396.    printf("\nEnter hard disk passphrase: ");
  397.    getkeydf(key,check,buf+7);
  398.    if(!df10 && !memcmp(Old13Check,buf+7,4))
  399.     {
  400.      printf("Check bytes in Disk %c: Boot Sector need updating from 1.3 to 1.1/1.3A. Proceed? ",
  401.             drvltr);
  402.      if(getyn())
  403.       {
  404.        memcpy(buf+7,check,4);
  405.        writesec(drive,firsthead,firstcyl,1,1,buf);
  406.       }
  407.     }
  408.   }
  409.  
  410.  if(memcmp(check,buf+7,4))
  411.   {
  412.    printf("Wrong passphrase.\n");
  413.    exit(1);
  414.   }
  415.  
  416.  en_key_idea((word16 *)key,expkey);
  417.  
  418.  memcpy(cryptdata->hkey,expkey,104);
  419.  memcpy(cryptdata->hkeychk,check,4);
  420.  cryptdata->hkeyv10=(char) df10;
  421. }
  422.  
  423. bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  424. /*set all slots inactive*/
  425. for (i=0;i<MAXDRV;i++)
  426.  {
  427.    if (cryptdata->hd[i].active)
  428.     printf("Drive %c: set to Safe mode.\n",cryptdata->hd[i].drvltr);
  429.    cryptdata->hd[i].active=0;
  430.  }
  431.  
  432. /*find empty or duplicate HD slot*/
  433. for (i=0;i<MAXDRV;i++)
  434.  {
  435.    if (  cryptdata->hd[i].dddrv    == 0  ||
  436.         (cryptdata->hd[i].dddrv    == drive &&
  437.          cryptdata->hd[i].firstcyl == firstcyl) )
  438.     {
  439.      cryptdata->hd[i].dddrv=drive;
  440.      cryptdata->hd[i].drvltr=drvltr;
  441.      cryptdata->hd[i].firstcyl=firstcyl;
  442.      cryptdata->hd[i].firsthd=firsthead;
  443.      cryptdata->hd[i].firstsec=1;
  444.      cryptdata->hd[i].lastcyl=firstcyl+maxcyl;
  445.      cryptdata->hd[i].maxsec=maxsector;
  446.      cryptdata->hd[i].maxhd=maxhead;
  447.      cryptdata->hd[i].secsize=secsize;
  448.      if(!safemode)
  449.       {
  450.        cryptdata->hd[i].serial[0]=serial[0];
  451.        cryptdata->hd[i].serial[1]=serial[1];
  452.        cryptdata->hd[i].active=1;
  453.        printf("\nHard disk %s encryption enabled logical drive %c:.\n",
  454.               df10 ? "(V 1.0)" : "(V 1.1)",
  455.               drvltr);
  456.        if (pgpsw && pgpv == NULL && setenv("PGPPASS",pass1) == 0)
  457.         printf("PGPPASS set. \n");
  458.        break;
  459.       }
  460.      else
  461.       {
  462.        printf("\nHard disk set to safe mode.\n");
  463.        break;
  464.       }
  465.     }
  466.  }
  467.  
  468. if (i == MAXDRV)
  469.  printf("\nERROR: More than %d Encrypted Partitions not supported\n",MAXDRV);
  470.  
  471. for(i=0;i<16;i++) key[i]='\0';
  472. for(i=0;i<52;i++) expkey[i]=0;
  473.  
  474. bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  475. clrbufs();
  476. return(0);
  477. }
  478.  
  479.